#!/bin/sh

##############################################################################
# Scala test runner 2.8.0
##############################################################################
# (c) 2002-2011 LAMP/EPFL
#
# This is free software; see the distribution for copying conditions.
# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
##############################################################################

cygwin=false;
darwin=false;
case "`uname`" in
    CYGWIN*) cygwin=true ;;
    Darwin*) darwin=true ;;
esac

# Finding the root folder for this Scala distribution
SOURCE=$0;
SCRIPT=`basename "$SOURCE"`;
while [ -h "$SOURCE" ]; do
    SCRIPT=`basename "$SOURCE"`;
    LOOKUP=`ls -ld "$SOURCE"`;
    TARGET=`expr "$LOOKUP" : '.*-> \(.*\)$'`;
    if expr "${TARGET:-.}/" : '/.*/$' > /dev/null; then
        SOURCE=${TARGET:-.};
    else
        SOURCE=`dirname "$SOURCE"`/${TARGET:-.};
    fi;
done;

# see #2092
SCALA_HOME=`dirname "$SOURCE"`
SCALA_HOME=`cd "$SCALA_HOME"; pwd -P`
SCALA_HOME=`cd "$SCALA_HOME"/..; pwd`

if $cygwin; then
    SCALA_HOME=`cygpath --windows --short-name "$SCALA_HOME"`
    SCALA_HOME=`cygpath --unix "$SCALA_HOME"`
fi

# Constructing the extension classpath
EXT_CLASSPATH=""
if [ -z "$EXT_CLASSPATH" ] ; then
    if [ -f "$SCALA_HOME/lib/scala-partest.jar" ] ; then
        for ext in "$SCALA_HOME"/lib/* ; do
            if [ -z "$EXT_CLASSPATH" ] ; then
                EXT_CLASSPATH="$ext"
            else
                EXT_CLASSPATH="$EXT_CLASSPATH:$ext"
            fi
        done
    elif [ -f "$SCALA_HOME/build/pack/lib/scala-partest.jar" ] ; then
        for lib in `echo "scala-partest scala-library scala-reflect scala-compiler diffutils"`; do
            ext="$SCALA_HOME/build/pack/lib/$lib.jar"
            if [ -z "$EXT_CLASSPATH" ] ; then
                EXT_CLASSPATH="$ext"
            else
                EXT_CLASSPATH="$EXT_CLASSPATH:$ext"
            fi
        done
    fi
fi

if $cygwin; then
    if [ "$OS" = "Windows_NT" ] && cygpath -m .>/dev/null 2>/dev/null ; then
        format=mixed
    else
        format=windows
    fi
    SCALA_HOME=`cygpath --$format "$SCALA_HOME"`
    EXT_CLASSPATH=`cygpath --path --$format "$EXT_CLASSPATH"`
fi

# last arg wins, so if JAVA_OPTS already contains one of these options
# the supplied argument will be used.
# At this writing it is reported test/partest --all requires 108m permgen.
JAVA_OPTS="-Xmx1024M -Xms64M -XX:MaxPermSize=128M $JAVA_OPTS"

partestDebugStr=""
if [ ! -z "${PARTEST_DEBUG}" ] ; then
  partestDebugStr="-Dpartest.debug=${PARTEST_DEBUG}"
fi

${JAVACMD:=java} \
  $JAVA_OPTS -cp "$EXT_CLASSPATH" \
  ${partestDebugStr} \
  -Dscala.home="${SCALA_HOME}" \
  -Dpartest.javacmd="${JAVACMD}" \
  -Dpartest.java_opts="${JAVA_OPTS}" \
  -Dpartest.scalac_opts="${SCALAC_OPTS}" \
  -Dpartest.javac_cmd="${JAVA_HOME}/bin/javac" \
  scala.tools.partest.nest.NestRunner "$@"
